package com.scada.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.scada.dao.IMemberBuyServiceDao;
import com.scada.dao.IMemberInfoDao;
import com.scada.inf.MemberInfoVisitor;
import com.scada.model.bean.MemberCustType;
import com.scada.model.bean.MemberInfo;
import com.scada.model.bean.MemberIntentionSer;
import com.scada.model.bean.ServiceInfo;
import com.scada.model.bean.vo.IntentionServiceNameVO;
import com.scada.model.bean.vo.MemberInfoVO;
import com.scada.model.bean.vo.ReturnTimeEdit;
import com.scada.model.result.ResponseBean;
import com.scada.model.search.MemberInfoSearch;

@Service("memberInfoVisitorService")
@WebService
@SOAPBinding(style = Style.RPC)
public class MemberInfoVisitorImpl implements MemberInfoVisitor{

	@Autowired
	private IMemberInfoDao memberInfoDao;
	@Autowired
	private IMemberBuyServiceDao memberBuyServiceDao;
	
	@Transactional
	@Override
	public void deleteMemberInfoById(String isShow, String ids) {
		
		try {
			Map<String,Object> map = null;
			for(String id : ids.split(",")) {
				map = new HashMap<String,Object>(); 
				map.put("isShow", isShow);
				map.put("id", Long.parseLong(id));
				memberInfoDao.deleteMemberInfoById(map);
			}
		}catch (Exception e) {
			throw new RuntimeException();
		}
	}

	@Transactional
	@Override
	public void insertMemberInfo(MemberInfo memberInfo, String intentionService, String custTypeStr) {
		
		try{
			memberInfoDao.insertMemberInfo(memberInfo);
			List<MemberIntentionSer> list = new ArrayList<MemberIntentionSer>();
			List<MemberCustType> custTypelist = new ArrayList<MemberCustType>();
			if(StringUtils.isNotBlank(intentionService) && !StringUtils.equals("null", intentionService)) {
				MemberIntentionSer mis = null;
				for(String serviceId : intentionService.split(",")){
					mis = MemberIntentionSer.builder().memberId(memberInfo.getId()).serviceId(Long.parseLong(serviceId)).build();
					list.add(mis);
				}
				memberInfoDao.insertMemberIntentionSer(list);
			}
			
			if(StringUtils.isNotBlank(custTypeStr) && !StringUtils.equals("null", custTypeStr)) {
				MemberCustType mct = null;
				for(String custType : custTypeStr.split(",")){
					mct = MemberCustType.builder().memberId(memberInfo.getId()).custType(Integer.parseInt(custType)).build();
					custTypelist.add(mct);
				}
				memberInfoDao.insertMemberCustType(custTypelist);
			}
		}catch(Exception e){
			throw new RuntimeException();
		}
	}

	@Override
	public ResponseBean getMemberInfoById(Long id) {
		
		ResponseBean responseBean = new ResponseBean();
		try{
			MemberInfoVO mi = memberInfoDao.getMemberInfoById(id);
			if(mi != null) {
				mi.setIntentionService(memberInfoDao.getMemberIntentionSerByMemberId(id));
				mi.setCustType(memberInfoDao.getMemberCustTypeByMemberId(id));
			}
			responseBean.addObject("result", mi);
			responseBean.success();
		}catch(Exception e){
			responseBean.failed();
		}
		return responseBean;
	}

	@Transactional
	@Override
	public void updateMemberInfo(MemberInfo memberInfo, String intentionService, String custTypeStr) {
		
		try{
			memberInfoDao.updateMemberInfo(memberInfo);
			memberInfoDao.deleteMemberIntentionSerByMemberId(memberInfo.getId());
			memberInfoDao.deleteMemberCustTypeByMemberId(memberInfo.getId());
			List<MemberIntentionSer> list = new ArrayList<MemberIntentionSer>();
			List<MemberCustType> custTypelist = new ArrayList<MemberCustType>();
			if(StringUtils.isNotBlank(intentionService) && !StringUtils.equals("null", intentionService)) {
				MemberIntentionSer mis = null;
				for(String serviceId : intentionService.split(",")){
					mis = MemberIntentionSer.builder().memberId(memberInfo.getId()).serviceId(Long.parseLong(serviceId)).build();
					list.add(mis);
				}
				memberInfoDao.insertMemberIntentionSer(list);
			}
			
			if(StringUtils.isNotBlank(custTypeStr) && !StringUtils.equals("null", custTypeStr)) {
				MemberCustType mct = null;
				for(String custType : custTypeStr.split(",")){
					mct = MemberCustType.builder().memberId(memberInfo.getId()).custType(Integer.parseInt(custType)).build();
					custTypelist.add(mct);
				}
				memberInfoDao.insertMemberCustType(custTypelist);
			}
		}catch(Exception e){
			throw new RuntimeException();
		}
	}

	@Override
	public ResponseBean getMemberInfoPageData(MemberInfoSearch memberInfoSearch) {
		
		ResponseBean responseBean = new ResponseBean();
		Map<String,Object> map = new HashMap<String,Object>();
		try{
			map.put("isShow", MemberInfo.IsShowCode.YES.getKey());
			map.put("memberName", memberInfoSearch.getMemberName());
			map.put("enName", memberInfoSearch.getEnName());
			map.put("sex", memberInfoSearch.getSex());
			map.put("mobile", memberInfoSearch.getMobile());
			map.put("wechatNum", memberInfoSearch.getWechatNum());
			map.put("custType", memberInfoSearch.getCustType());
			map.put("ifBuyService", memberInfoSearch.getIfBuyService());
			responseBean.addObject("resultCount", memberInfoDao.getMemberInfoPageCount(map));
			map.put("start", memberInfoSearch.getStart());
			map.put("pageSize", memberInfoSearch.getLength());
            responseBean.addObject("resultList", memberInfoDao.getMemberInfoPageList(map));
			responseBean.success();
		}catch(Exception e){
			responseBean.failed();
		}
		return responseBean;
	}

	@Override
	public ResponseBean getSelectMemberData(Integer pageSize, Integer currentPage, String searchName) {
		
		ResponseBean rb=new ResponseBean();
		try{
			Map<String,Object> map=new HashMap<String,Object>();
			map.put("searchName", searchName);
			map.put("isShow", MemberInfo.IsShowCode.YES.getKey());
			rb.addObject("resultCount", memberInfoDao.getSelectMemberCount(map));
			map.put("start", currentPage*pageSize);
			map.put("pageSize", pageSize);
			rb.addObject("resultList", memberInfoDao.getSelectMemberList(map));
			rb.success();
		}catch(Exception e){
			rb.failed(e.getMessage());
		}
		return rb;
	}

	@Override
	public ResponseBean getMemberInfoDetail(Long id) {
		
		ResponseBean responseBean = new ResponseBean();
		try{
			MemberInfoVO mi = memberInfoDao.getMemberInfoDetail(id);
			if(mi != null) {
				IntentionServiceNameVO isn = memberInfoDao.getIntentionSerNameByMemberId(id);
				if(isn != null) {
					mi.setIntentionService(isn.getServiceName());
					mi.setIntentionServiceType(isn.getInvestmentTypeDesc());
				}
				mi.setCustType(memberInfoDao.getMemberCustTypeName(id));
				Map<String,Object> map = new HashMap<String,Object>();
				map.put("memberId", mi.getId());
				map.put("serviceType", ServiceInfo.ServiceTypeCode.MEMBER.getKey());
				mi.setMemberServiceBuy(memberBuyServiceDao.getMemberBuyServicName(map));
				map.put("serviceType", ServiceInfo.ServiceTypeCode.PROJECT.getKey());
				mi.setProjectServiceBuy(memberBuyServiceDao.getMemberBuyServicName(map));
				map.put("serviceType", "");
				mi.setAlreadyInvestmentType(memberBuyServiceDao.getMemberBuyServiceType(map));
			}
			responseBean.addObject("result", mi);
			responseBean.success();
		}catch(Exception e){
			responseBean.failed();
		}
		return responseBean;
	}

	
	@Override
	public ResponseBean validateMemberRepeat(String mobile, String email1, Long id) {
		
		ResponseBean responseBean = new ResponseBean();
		Map<String,Object> map = new HashMap<String,Object>();
		try{
			map.put("isShow", MemberInfo.IsShowCode.YES.getKey());
			map.put("mobile", mobile);
			map.put("email1", email1);
			map.put("id", id);
            responseBean.addObject("result", memberInfoDao.validateMemberRepeat(map));
			responseBean.success();
		}catch(Exception e){
			responseBean.failed();
		}
		return responseBean;
	}

	@Override
	public ResponseBean updateReturnTime(ReturnTimeEdit returnTimeEdit) {
		
		ResponseBean responseBean = new ResponseBean();
		Map<String,Object> map = new HashMap<String,Object>();
		try{
			map = new HashMap<String,Object>(); 
			map.put("revisitDate", returnTimeEdit.getRevisitDate());
			map.put("id", returnTimeEdit.getMemberId());
			memberInfoDao.updateReturnTime(map);
			responseBean.success();
		}catch(Exception e){
			responseBean.failed();
		}
		return responseBean;
	}

}
